package com.shanzhu.book.service.impl;

import com.shanzhu.book.mapper.SeatReservationMapper;
import com.shanzhu.book.mapper.UserMapper;
import com.shanzhu.book.model.SeatReservation;
import com.shanzhu.book.model.User;
import com.shanzhu.book.mq.SeatMessageProducer;
import com.shanzhu.book.service.SeatReservationService;
import com.shanzhu.book.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SeatReservationServiceImpl implements SeatReservationService {

    @Autowired
    private SeatReservationMapper seatReservationMapper;

    @Autowired
    private SeatMessageProducer seatMessageProducer;

    @Autowired
    private UserMapper userMapper;

    @Override
    public boolean reserveSeat(SeatReservation reservation) {
        try {
            // 设置默认值
            reservation.setCreateTime(new Date());
            reservation.setUpdateTime(new Date());
            
            // 发送到消息队列异步处理
            seatMessageProducer.sendReservationMessage(reservation);
            return true;
        } catch (Exception e) {
            throw new RuntimeException("Failed to reserve seat", e);
        }
    }

    @Override
    public int selectCount() {
        return seatReservationMapper.selectCount();
    }

    @Override
    public List<SeatReservation> searchReservations(String seatNumber, String location, Integer integer, int page, int limit) {
        return null;
    }

    @Override
    public List<SeatReservation> searchReservations2(Map<String, Object> params) {
        return seatReservationMapper.selectBySearch2(params);
    }

    @Override
    public int selectCountBySearch(String seatNumber, String location, Object o, Integer integer, Object o1, Object o2) {
        return 0;
    }

    @Override
    public int getSearchCount(Map<String, Object> params) {
        return seatReservationMapper.selectCountBySearch2(params);

    }

    @Override
    public Integer deleteSeatReservation(SeatReservation seat) {
        Map<String, Object> map = new HashMap<>();
        map.put("noticeid", seat.getId());
//        if(seatReservationMapper.selectCountBySearch2(map) > 0) {
//            return -1;
//        }
        return seatReservationMapper.deleteByPrimaryKey(seat.getId());
    }

    @Override
    public Integer addSeat(SeatReservation notice) {
        notice.setCreateTime(new Date());
//        notice.set
        User user = userMapper.selectByUsername(notice.getUsername());
        if (user==null){
            return -1;
        }
        notice.setUserId(Long.valueOf(user.getUserid()));
        return seatReservationMapper.insert(notice);
    }

    @Override
    public Integer updateSeat(SeatReservation seat) {
        return seatReservationMapper.updateByPrimaryKeySelective(seat);
    }

    @Override
    public Integer updateSeatStatus(SeatReservation seat) {

        return seatReservationMapper.updateByPrimaryKeySelective(seat);
    }

    @Override
    public boolean cancelReservation(Long reservationId, Long userId) {
        SeatReservation reservation = seatReservationMapper.selectByPrimaryKey(reservationId);
        
        if (reservation == null || !reservation.getUserId().equals(userId)) {
            return false;
        }
        
        if (reservation.getReserveStatus() != 1) {
            return false; // 只能取消已预约状态
        }
        
        seatMessageProducer.sendCancelMessage(reservationId);
        return true;
    }

    @Override
    public List<SeatReservation> getUserReservations(Long userId) {
        return seatReservationMapper.selectByUserId(userId);
    }

    @Override
    public SeatReservation getCurrentReservation(Long userId) {
        return seatReservationMapper.selectCurrentReservationByUserId(userId);
    }

//    @Override
//    public PageInfo<SeatReservation> searchReservations(String seatNumber, String location,
//                                                      Integer status, int pageNum, int pageSize) {
//        PageHelper.startPage(pageNum, pageSize);
//        List<SeatReservation> list = seatReservationMapper.selectBySearch(
//            seatNumber, location, null, status, null, null, 0, 0);
//        return new PageInfo<>(list);
//    }

    @Override
    public boolean checkIn(Long reservationId, Long userId) {
        SeatReservation reservation = seatReservationMapper.selectByPrimaryKey(reservationId);
        
        if (reservation == null || !reservation.getUserId().equals(userId)) {
            return false;
        }
        
        if (reservation.getReserveStatus() != 1) {
            return false; // 只能签入已预约状态
        }
        
        reservation.setReserveStatus(2); // 使用中
        reservation.setActualCheckin(new Date());
        reservation.setUpdateTime(new Date());
        
        return seatReservationMapper.updateByPrimaryKeySelective(reservation) > 0;
    }

    @Override
    public boolean checkOut(Long reservationId, Long userId) {
        SeatReservation reservation = seatReservationMapper.selectByPrimaryKey(reservationId);
        
        if (reservation == null || !reservation.getUserId().equals(userId)) {
            return false;
        }
        
        if (reservation.getReserveStatus() != 2) {
            return false; // 只能签出使用中状态
        }
        
        reservation.setReserveStatus(0); // 重置为可预约
        reservation.setActualCheckout(new Date());
        reservation.setUpdateTime(new Date());
        
        return seatReservationMapper.updateByPrimaryKeySelective(reservation) > 0;
    }
}